package top.xia17.plugins.demo.config;


import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import top.xia17.plugins.fast.common.spring.converter.LocalDateConverter;
import top.xia17.plugins.fast.common.spring.converter.LocalDateTimeConverter;
import top.xia17.plugins.fast.common.spring.converter.StringToBooleanConverter;
import top.xia17.plugins.fast.common.spring.interceptor.VisitInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * mvc配置类
 *
 * @author xia17
 */
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig extends WebMvcConfigurationSupport {

    private final GlobalProperties globalProperties;

    /**
     * 添加拦截器
     *
     * @param registry 拦截器注册类
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //addInterceptor  添加拦截器，参数需要实现HandlerInterceptor
        //addPathPatterns  拦截那些访问地址 /** 表示所有
        //excludePathPatterns   哪些访问不拦截 这里不拦截静态资源
        registry.addInterceptor(new VisitInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/css/**", "/img/**", "/js/**");
    }


    /**
     * 建议使用该方法进行HttpMessageConverters的修改，此时的converters已经是Spring初始化过的
     * 因为加入了WebMvcConfigure，导致Spring的HttpMessageConverters中的String转换类默认使用ISO-8859-1编码
     * 所以这里对Spring已经初始化过的StringHttpMessageConverter的编码进行修改
     *
     * @param converters /
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.forEach(converter -> {
            // 解决返回String类型时中文乱码
            if (converter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8);
            }
            // 解决返回Long类型字段精度丢失
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter c = (MappingJackson2HttpMessageConverter) converter;
                SimpleModule module = new SimpleModule();
                module.addSerializer(Long.class, ToStringSerializer.instance);
                module.addSerializer(Long.TYPE, ToStringSerializer.instance);
                c.getObjectMapper().registerModule(module);
            }
        });
    }

    /**
     * 添加 访问参数的 转化器 这里加的日期转化
     *
     * @param registry 参数格式化注册器
     */
    @Override
    protected void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new LocalDateTimeConverter());
        registry.addConverter(new LocalDateConverter());
        registry.addConverter(new StringToBooleanConverter());
    }


}
